package com.jrsoft.bim.util;

import org.apache.commons.compress.utils.IOUtils;
import org.springframework.util.FileCopyUtils;

import java.io.*;

/**
 * ${DESCRIPTION}
 *
 * @author Blueeyedboy
 * @create 2019-12-20 11:31 AM
 **/
public class FileUtil {

	private final static String EXCEL2003 = "xls";
	private final static String EXCEL2007 = "xlsx";
	/**
	 * 获取文件后缀名
	 * @param fileName
	 * @return java.lang.String
	 * @author Blueeyedboy
	 * @create 2017/5/23 10:21
	 **/
	public static String getSuffix(String fileName){

		String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
		return prefix;
	}

	public static boolean isExcel(String fileName){
		String suffix = getSuffix(fileName).toLowerCase();
		if(suffix.equals(EXCEL2003) || suffix.equals(EXCEL2007)){
			return true;
		}
		return false;
	}

	/**
	 * 文件拷贝
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 9:41
	 **/
	public static void copyFile(String srcPath, String desPath) throws RuntimeException, FileNotFoundException {
		copyFile(new FileInputStream(srcPath), new FileOutputStream(desPath));
	}
	/**
	 * 文件拷贝
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 9:41
	 **/
	public static void copyFile(File src, File des) throws RuntimeException, FileNotFoundException {
		copyFile(new FileInputStream(src), new FileOutputStream(des));
	}

	/**
	 * 文件拷贝
	 * @param
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/5/23 9:41
	 **/
	public static void copyFile(InputStream in, OutputStream out) throws RuntimeException {
		try {
			FileCopyUtils.copy(in, out);
		}catch(Exception e){
			e.printStackTrace();;
			throw new RuntimeException("文件拷贝失败！");
		}finally {
			IOUtils.closeQuietly(in);
			IOUtils.closeQuietly(out);
		}
	}

	/**
	 * 如果flag == true，清空内容重新写，否则追加到末尾
	 *
	 * @param filePath
	 * @param content
	 * @param flag
	 */
	public static void writeToFile(String filePath, String content, boolean flag) {
		File file = new File(filePath);
		// System.out.println(file.exists());
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			file.delete();
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// System.out.println("222"+file.getAbsolutePath());

		if (flag == false) {
			try {
				// 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
				FileWriter writer = new FileWriter(file, true);
				writer.write(content);
				writer.close();

			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {

			// System.out.println("---"+content);
			try {
				FileOutputStream os = new FileOutputStream(file);
				//os.write(URLDecoder.decode(content, "utf-8").getBytes("utf-8"));
				// fw.write(URLDecoder.decode(attachment.getContent(), "utf-8"));
				os.write(content.getBytes());
				os.flush();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		file.setWritable(false);
	}
	// 按行读文件
	public static String readFile(String fileDir) {
		StringBuilder resultString = new StringBuilder();
		try {
			// FileReader myFileReader=new FileReader("D:/haha/你好.doc");
			FileReader myFileReader = new FileReader(fileDir);
			BufferedReader myBufferedReader = new BufferedReader(myFileReader);
			String myString = null;

			while ((myString = myBufferedReader.readLine()) != null) {
				resultString.append(myString);//.append("\n");
			}
			myFileReader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString.toString();
	}
}
